/* 
 * File:   TestKnotClass.cpp
 * Author: vicror
 * 
 * Created on October 15, 2011, 12:25 AM
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "TestKnotClass.h"
#include "../Item.h"


using SearchEnginer::Item;

CPPUNIT_TEST_SUITE_REGISTRATION(TestKnotClass);

TestKnotClass::TestKnotClass() {
}

TestKnotClass::~TestKnotClass() {
}

void TestKnotClass::setUp() {
}

void TestKnotClass::tearDown() {
}

void TestKnotClass::testGeneral() {
    const int count = 19;
    int maIndex[count]={2,2,2,17,125,17,44,44,44,17,55,55,3,3,3,5,5,5,5};
    int maStep[count] ={2,3,4, 4,  4, 2, 4, 3, 2, 3, 1, 6,2,3,4,4,3,2,1};
    int i=0;
    for(; i < count; i++) {
        CPPUNIT_ASSERT(a.append(Item(maIndex[i],maStep[i])) == i+1);
    }

    CPPUNIT_ASSERT(a.append(Item(2,2)) == i);
    CPPUNIT_ASSERT(a.append(Item(2,3)) == i);
    CPPUNIT_ASSERT(a.append(Item(44,3)) == i);
    b=a;
    CPPUNIT_ASSERT(b.hasItem(Item(17,2)));
    CPPUNIT_ASSERT(b.hasItem(Item(17,3)));
    CPPUNIT_ASSERT(!b.hasItem(Item(17,5)));
    SearchEnginer::Knot ce(b);
    CPPUNIT_ASSERT(ce.hasItem(Item(55,1)));
    CPPUNIT_ASSERT(ce.hasItem(Item(55,6)));
    CPPUNIT_ASSERT(!ce.hasItem(Item(55,5)));
    c.copy(a);
    CPPUNIT_ASSERT(c.hasItem(Item(2,2)));
    CPPUNIT_ASSERT(c.hasItem(Item(2,3)));
    CPPUNIT_ASSERT(!c.hasItem(Item(101,5)));
    CPPUNIT_ASSERT(c.hasItem(Item(125,4)));
    c.remove(Item(17,3));
    CPPUNIT_ASSERT(c.size() == i-1);
    c.remove(Item(2,2));
    CPPUNIT_ASSERT(c.size() == i-2);
    c.remove(Item(125,4));
    CPPUNIT_ASSERT(c.size() == i-3);
    CPPUNIT_ASSERT(!c.hasItem(Item(17,3)));
    CPPUNIT_ASSERT(!c.hasItem(Item(2,2)));
    CPPUNIT_ASSERT(!c.hasItem(Item(125,4)));

    c.clear();
    CPPUNIT_ASSERT(c.size() == 0);
}

void TestKnotClass::testLogic() {
    const int counta = 19;
    const int countb = 9;
    a.clear();
    b.clear();
    int maIndexa[counta]={2,2,2,3,3,3,5,5, 5, 5,17,17,17,44,44,44,55,55,125};
    int maStepa[counta] ={2,3,4,1,2,3,7,9,17,18, 2, 5, 7, 4, 9,15, 7, 3, 12};
    int maIndexb[countb]={2,2,2,                17,17,   44,44,44,      125};
    int maStepb[countb] ={2,3,4,                 4, 6,    2, 3, 5,       13};
    int i=0;
    for(; i < counta; i++) {
        a.append(Item(maIndexa[i],maStepa[i]));
    }
    for(i=0; i < countb; i++) {
        b.append(Item(maIndexb[i],maStepb[i]));
    }
    c=a;
    CPPUNIT_ASSERT(a.append(b) == 25);
    CPPUNIT_ASSERT(c.andKnot(b,1) == 5);
    SearchEnginer::Knot::ListItemTItr itr = a.getSearchItr(Item(2,2));
    CPPUNIT_ASSERT((*itr).index() == 2);
    CPPUNIT_ASSERT((*itr).step() == 2);

}
